package com.leetcode.partition14;

/**
 * @author rkc
 * @date 2022/8/26 9:05
 */
public class LC1343大小为K且平均值大于等于阈值的子数组数目 {

    private final int N = 100010;
    private int[] sum = new int[N];
    private int n;

    public int numOfSubarrays(int[] arr, int k, int threshold) {
        n = arr.length;
        for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + arr[i - 1];
        int ans = 0;
        for (int i = 0; i <= n - k; i++) {
            int l = i + 1, r = i + k;
            if (get(l, r) >= threshold * k) {
                ans++;
            }
        }
        return ans;
    }

    private int get(int l, int r) {
        return sum[r] - sum[l - 1];
    }
}
